<?php
namespace app\index\controller;
use app\common\model\Category;
use houdunwang\arr\Arr;
use app\common\model\Goods;
use think\Controller;
class Lists extends Common
{
    public function index(Category $category)
    {

//        $cartdata = $this->category();

        $cdata = db('category')->limit(5)->select();
        $field = Arr::channelLevel(db('category')->where('id>5')->select(), $pid = 0, $html = "&nbsp;", $fieldPri = 'id', $fieldPid = 'pid');
        #获取传过来的栏目id
        $id = input('id');
        #根据id处理面包屑导航(递归找父级)
        $Fdata = $this->getFdata($category, $id);
//        halt($Fdata);
        #查询当前栏目下的所有的子分类数据
        $Sdata = $this->getSdata($category, $id);
//        halt($Sdata);
        #查询所有的分类数据
        $cateAllData = db('category')->select ();
        $cateAllData = $cateAllData ? : '底下无数据';
//        halt($cateAllData);
        return $this->template('lists',compact('field','cdata','ldata','Fdata','cateAllData','Sdata',''));
    }

    /**
     * 获得所有父级栏目
     * @param $category
     * @param $id
     * @return array
     *
     */
     public function getFdata($category,$id)
     {
        $data = db('category')->select();
//        halt($data);
        $Fdata = $category->getFatherData($data,$id);
        #将数组反转
//         halt($Fdata);
        return array_reverse($Fdata);
     }

    /**
     * 子集栏目数据
     * 获得
     * @param $category
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     *
     */
      public function getSdata($category,$id)
      {
          $Sdata = $category->getSon(db('category')->select(),$id);
//          halt($Sdata);
          #将自己算进去
          $Sdata[] = $id*1;
//          halt($Sdata);

//          return (new Goods())->whereIn('cid',$Sdata)->select();
//          return Goods::whereIn('cid',$Sdata)->select();
         return Goods::where('cid','in',$Sdata)->select() ;
      }
}
